
#include <core/timer.h>
#include <core/init.h>
#include <core/errno.h>
#include <flash/flash.h>


static struct Flash *g_pvFlashDevice[MAX_FLASH_DEVICES];


int GkFlashRegister(struct Flash *pFlash)
{
	int i;

	for (i = 0; i < MAX_FLASH_DEVICES; i++)
	{
		if (NULL == g_pvFlashDevice[i]) 
		{
			g_pvFlashDevice[i] = pFlash;

			return i;
		}
	}

	return -EBUSY;
}


int GkFlashUnregister (struct Flash *pFlash)
{
	int i;

	for (i = 0; i < MAX_FLASH_DEVICES; i++)
	{
		if (pFlash == g_pvFlashDevice[i]) 
		{
			g_pvFlashDevice[i] = NULL;

			return i;
		}
	}

	return -ENODEV;
}


struct Flash *GkFlashGet(unsigned int num)
{
	struct Flash *pFlash;

	if (num >= MAX_FLASH_DEVICES)
		return NULL;

	pFlash = g_pvFlashDevice[num];

	return pFlash;
}


struct Flash *GkFlashGetEx(const char *name)
{
	int i;
	struct Flash *pFlash = NULL;

	for (i = 0; i < MAX_FLASH_DEVICES; i++)
	{
		if (g_pvFlashDevice[i] && !strcmp(name, g_pvFlashDevice[i]->szName))
		{
			pFlash = g_pvFlashDevice[i];
			break;
		}
	}

	return pFlash;
}


static __INIT__ int FlashCoreInit(void)
{
	int i;

	SUBSYS_INFO("Flash Subsystem.\n");

	for (i = 0; i < MAX_FLASH_DEVICES; i++)
		g_pvFlashDevice[i] = NULL;

	return 0;
}


SUBSYS_INIT(FlashCoreInit);

